package com.mobis.user.service;

import com.mobis.user.dao.AdminDao;
import com.mobis.user.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import utils.IdWorker;

@Service
public class AdminService {

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private IdWorker idWorker;

    public void AddAdmin(Admin admin){

        //设置主键
        admin.setId(idWorker.nextId()+"");

        //对密码进行加密
        String password=admin.getPassword();
        password=bCryptPasswordEncoder.encode(password);
        admin.setPassword(password);

        adminDao.save(admin);

    }

    public Admin findByNameAndPassword(String name,String password){

        Admin sqladmin=adminDao.findAdminByLoginname(name);
        if (null==sqladmin){
            return null;
        }

        if (bCryptPasswordEncoder.matches(password,sqladmin.getPassword())){
            return  sqladmin;
        }
        return  null;
    }
}
